import { _decorator, Color, Component, instantiate, Node, Prefab, Sprite, SpriteFrame, UITransform } from 'cc';
import { UIManager } from '../common/UIManager';
import { PrefabEnum } from '../common/PrefabEnum';
import { UserInfo } from '../common/UserInfo';
import { LocationType, Soldier } from '../entity/Soldier';
import { SoldierCover } from './SoldierCover';
import { EventManager } from '../event/EventManager';
import { SoldierTrainItem } from './SoldierTrainItem';
import VillageApi from '../api/VillageApi';
import { buildingType, EventType } from '../settings/const';
import { TrainEvent } from '../entity/TrainEvent';
import { TrainTabType } from '../entity/TrainTabType';
import { UIHeader } from '../common/UIHeader';
import { TabLayout } from '../common/TabLayout';
import { Utils } from '../common/Utils';
import { BManager } from '../entity/BManager';
const { ccclass, property } = _decorator;

@ccclass('TrainIndex')
export class TrainIndex extends Component {

    public static instance: TrainIndex
    @property(Node)
    bg:Node

    @property(UIHeader)
    uiHeader: UIHeader


    @property(Node)
    coverContent: Node


    @property(Prefab)
    SoldierCover: Prefab
    @property(Prefab)
    soldierTrainItem: Prefab
    @property(SpriteFrame)
    btnSpriteFrame1: SpriteFrame
    @property(SpriteFrame)
    btnSpriteFrame2: SpriteFrame

    public trainTabType: TrainTabType

    private soldierList: Soldier[] = []


    protected onLoad(): void {
        TrainIndex.instance = this
    }

    start() {
        this.bg.on(Node.EventType.TOUCH_END, this.onBg, this)

        let b = BManager.getBManageById(buildingType.B19_Barracks)

        if (b) {
            this.uiHeader.setData(`兵营 等级${b.grade}`)
            EventManager.instance.on(`兵营 等级${b.grade}close`, this.onClose, this)
        } else {
            this.uiHeader.setData(`兵营`)
            EventManager.instance.on(`兵营close`, this.onClose, this)
        }

        this.init()




    }

    async init() {

        await UIManager.instance.showLoading()
        await UserInfo.instance.refreshVillageDetail()

        this.onFoot()
        UIManager.instance.closeLoading()



    }

    protected onDisable(): void {

    }

    onFoot() {

        let list = Soldier.createByLocation(LocationType.Barracks)
        console.log(list)
        this.renderCoverList(list)
    }




    renderCoverList(list: Soldier[]) {
        this.soldierList = list
        this.coverContent.destroyAllChildren()
        for (let item of list) {
            let node = instantiate(this.SoldierCover)
            this.coverContent.addChild(node)
            node.getComponent(SoldierCover).setData(item)
        }



        UIManager.instance.closeUI(PrefabEnum.ProPanel)
    }





    onClose() {
        UIManager.instance.closeUI(PrefabEnum.TrainIndex)
        UIManager.instance.closeUI(PrefabEnum.TrainNumber)
    }

    onBg(){
        UIManager.instance.closeUI(PrefabEnum.TrainNumber)
    }



    // async updateTrainList() {
    //     this.trainListContent.destroyAllChildren()

    //     let res = await VillageApi.getVillageEvents(EventType.train)
    //     console.log(res)
    //     let tranEventList: TrainEvent[] = res.data
    //     for (let e of tranEventList) {
    //         let node = instantiate(this.soldierTrainItem)
    //         this.trainListContent.addChild(node)
    //         node.getComponent(SoldierTrainItem).setData(e)
    //     }
    // }

}


